package com.briup.estore.adminAPI.web.controller;

import com.briup.estore.common.bean.Banner;
import com.briup.estore.common.service.IBannerService;
import com.briup.estore.common.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author adam
 * @date 2022/10/31
 */
@RestController
@Api(tags = "轮播管理")
@RequestMapping("banner")
@CrossOrigin
public class BannerController {
    @Autowired
    private IBannerService bannerService;

    @ApiOperation("查询轮播信息带分页")
    @GetMapping("findBanners")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "轮播标题", required = false, paramType = "query"),
            @ApiImplicitParam(name = "active", value = "启用状态 传值String类型 的true 或者 false", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面显示数据条数", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"),
    })
    public Result findBanners(@RequestParam(required = false, defaultValue = "") String title, @RequestParam(required = false, defaultValue = "") String active, Integer pageSize, Integer pageNum) {

        try {
            Page<Banner> banners = bannerService.findAll(title, active, pageSize, pageNum - 1);
            return Result.success(banners);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(50000, e.getMessage());
        }

    }

    @PostMapping("saveBanner")
    @ApiOperation("新增轮播以及修改轮播信息")
    public Result saveBanner(@RequestBody Banner banner) {
        bannerService.save(banner);
        return Result.success();
    }

    @PutMapping("updateActive/{id}")
    @ApiOperation("修改轮播的启用状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", type = "path", required = true, value = "轮播编号")
    })
    public Result updateActive(@PathVariable Long id) {

        bannerService.updateActive(id);
        return Result.success();
    }


}
